﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
using Hi_Baidu.Packet;

namespace Hi_Baidu.UI.Compoment
{
	public partial class SendFileControl : IMetaData
    {
        public SendFileControl()
        {
            this.InitializeComponent();
            // 在此点之下插入创建对象所需的代码。
            this._SENDF_FILE_SESSION_CODE = Encrypt.Encrypt_HASH.HashString("SENDFILE:" + DateTime.Now.ToString());
            this.key = this._SENDF_FILE_SESSION_CODE;
        }

        public void SetFileSize()
        {
            if(!string.IsNullOrEmpty(this._FilePath))
            {
                FileInfo fileInfo = new FileInfo(this._FilePath);
                //Get the size of file (KB)
                this._FileSize = (int)fileInfo.Length;
            }
        }

        private InPackage _InPackage;
        /// <summary>
        ///     发送文件请求包
        /// </summary>
        public InPackage InPackage
        {
            get { return _InPackage; }
            set { _InPackage = value; }
        } 

        private ControlType _UseType = ControlType.Send;
        /// <summary>
        ///     当前控件使用类型
        /// </summary>
        public ControlType UseType
        {
            get {  return _UseType; }
            set
            {
                _UseType = value;
                //发送模式
                if (_UseType == ControlType.Send)
                {
                    SP_Send.Visibility = Visibility.Visible;
                    SP_Recv.Visibility = Visibility.Collapsed;
                    this.tb_Notify.Text = "发送文件请求 : ";
                }
                //接收模式
                else
                {
                    SP_Send.Visibility = Visibility.Collapsed;
                    SP_Recv.Visibility = Visibility.Visible;
                    this.tb_Notify.Text = "接收文件请求 : ";
                }
            }
        }

        /// <summary>
        ///     当前控件类型
        /// </summary>
        public enum ControlType
        {
            /// <summary>
            ///     发送
            /// </summary>
            Send,
            /// <summary>
            ///     接收
            /// </summary>
            Recv
        }

        private String _FilePath;
        /// <summary>
        ///     文件路径
        /// </summary>
        public String FilePath
        {
            get { return _FilePath; }
            set { _FilePath = value; }
        }

        private String _FileExt;
        /// <summary>
        ///     文件后缀名
        /// </summary>
        public String FileExt
        {
            get { return _FileExt; }
            set { _FileExt = value; }
        }


        private int _FileSize;
        /// <summary>
        ///     文件大小
        /// </summary>
        public int FileSize
        {
            get { return _FileSize; }
            set { _FileSize = value; }
        }

        private String _SENDF_FILE_SESSION_CODE;
        /// <summary>
        ///     获取本次传送文件的传送会话ID
        /// </summary>
        public String SENDF_FILE_SESSION_CODE
        {
            get { return _SENDF_FILE_SESSION_CODE; }
            set { _SENDF_FILE_SESSION_CODE = value; }
        }

        private String _FileName;
        /// <summary>
        ///     文件名
        /// </summary>
        public String FileName
        {
            get { return _FileName; }
            set
            {
                _FileName = value;
                this.Tb_FileName.Text = _FileName;
            }
        }
        private BitmapImage _FileIcon;
        /// <summary>
        ///     文件图标
        /// </summary>
        public BitmapImage FileIcon
        {
            get { return _FileIcon; }
            set 
            {
                _FileIcon = value;
                this.Img_FileIco.Source = _FileIcon;
            }
        }

        #region IMetaData 成员
        private String key;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        #endregion

        private void Tb_Cancle_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnCancleSendFile(new CancleSendFileEventArgs(this.key, this));
        }


        #region 'CancleSendFile' event definition code

        /// <summary>
        ///     EventArgs derived type which holds the custom event fields
        /// </summary>
        public class CancleSendFileEventArgs : System.EventArgs
        {
            /// <summary>
            ///     Use this constructor to initialize the event arguments
            ///     object with the custom event fields
            /// </summary>
            public CancleSendFileEventArgs(String strKey, SendFileControl senControl)
            {
                this.Key = strKey;
                this.Control = senControl;
            }

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly String Key;

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly SendFileControl Control;

        }

        // Private delegate linked list (explicitly defined)
        private EventHandler<CancleSendFileEventArgs> CancleSendFileEventHandlerDelegate;

        /// <summary>
        ///     TODO: Describe the purpose of this event here
        /// </summary>
        public event EventHandler<CancleSendFileEventArgs> CancleSendFile
        {
            // Explicit event definition with accessor methods
            add
            {
                CancleSendFileEventHandlerDelegate = (EventHandler<CancleSendFileEventArgs>)Delegate.Combine(CancleSendFileEventHandlerDelegate, value);
            }
            remove
            {
                CancleSendFileEventHandlerDelegate = (EventHandler<CancleSendFileEventArgs>)Delegate.Remove(CancleSendFileEventHandlerDelegate, value);
            }
        }

        /// <summary>
        ///     This is the method that is responsible for notifying
        ///     receivers that the event occurred
        /// </summary>
        protected virtual void OnCancleSendFile(CancleSendFileEventArgs e)
        {
            if (CancleSendFileEventHandlerDelegate != null)
            {
                CancleSendFileEventHandlerDelegate(this, e);
            }
        }

        #endregion //('CancleSendFile' event definition code)

        #region 'AllowFile' event definition code

        /// <summary>
        ///     EventArgs derived type which holds the custom event fields
        /// </summary>
        public class AllowFileEventArgs : System.EventArgs
        {
            /// <summary>
            ///     Use this constructor to initialize the event arguments
            ///     object with the custom event fields
            /// </summary>
            public AllowFileEventArgs(SendFileControl senControl)
            {
                this.Control = senControl;
            }

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly SendFileControl Control;

        }

        // Private delegate linked list (explicitly defined)
        private EventHandler<AllowFileEventArgs> AllowFileEventHandlerDelegate;

        /// <summary>
        ///     TODO: Describe the purpose of this event here
        /// </summary>
        public event EventHandler<AllowFileEventArgs> AllowFile
        {
            // Explicit event definition with accessor methods
            add
            {
                AllowFileEventHandlerDelegate = (EventHandler<AllowFileEventArgs>)Delegate.Combine(AllowFileEventHandlerDelegate, value);
            }
            remove
            {
                AllowFileEventHandlerDelegate = (EventHandler<AllowFileEventArgs>)Delegate.Remove(AllowFileEventHandlerDelegate, value);
            }
        }

        /// <summary>
        ///     This is the method that is responsible for notifying
        ///     receivers that the event occurred
        /// </summary>
        protected virtual void OnAllowFile(AllowFileEventArgs e)
        {
            if (AllowFileEventHandlerDelegate != null)
            {
                AllowFileEventHandlerDelegate(this, e);
            }
        }

        #endregion //('AllowFile' event definition code)

        #region 'ReSaveFile' event definition code

        /// <summary>
        ///     EventArgs derived type which holds the custom event fields
        /// </summary>
        public class ReSaveFileEventArgs : System.EventArgs
        {
            /// <summary>
            ///     Use this constructor to initialize the event arguments
            ///     object with the custom event fields
            /// </summary>
            public ReSaveFileEventArgs(SendFileControl senControl)
            {
                this.Control = senControl;
            }

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly SendFileControl Control;

        }

        // Private delegate linked list (explicitly defined)
        private EventHandler<ReSaveFileEventArgs> ReSaveFileEventHandlerDelegate;

        /// <summary>
        ///     TODO: Describe the purpose of this event here
        /// </summary>
        public event EventHandler<ReSaveFileEventArgs> ReSaveFile
        {
            // Explicit event definition with accessor methods
            add
            {
                ReSaveFileEventHandlerDelegate = (EventHandler<ReSaveFileEventArgs>)Delegate.Combine(ReSaveFileEventHandlerDelegate, value);
            }
            remove
            {
                ReSaveFileEventHandlerDelegate = (EventHandler<ReSaveFileEventArgs>)Delegate.Remove(ReSaveFileEventHandlerDelegate, value);
            }
        }

        /// <summary>
        ///     This is the method that is responsible for notifying
        ///     receivers that the event occurred
        /// </summary>
        protected virtual void OnReSaveFile(ReSaveFileEventArgs e)
        {
            if (ReSaveFileEventHandlerDelegate != null)
            {
                ReSaveFileEventHandlerDelegate(this, e);
            }
        }

        #endregion //('ReSaveFile' event definition code)

        #region 'PassFile' event definition code

        /// <summary>
        ///     EventArgs derived type which holds the custom event fields
        /// </summary>
        public class PassFileEventArgs : System.EventArgs
        {
            /// <summary>
            ///     Use this constructor to initialize the event arguments
            ///     object with the custom event fields
            /// </summary>
            public PassFileEventArgs(SendFileControl senControl)
            {
                this.Control = senControl;
            }

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly SendFileControl Control;

        }

        // Private delegate linked list (explicitly defined)
        private EventHandler<PassFileEventArgs> PassFileEventHandlerDelegate;

        /// <summary>
        ///     TODO: Describe the purpose of this event here
        /// </summary>
        public event EventHandler<PassFileEventArgs> PassFile
        {
            // Explicit event definition with accessor methods
            add
            {
                PassFileEventHandlerDelegate = (EventHandler<PassFileEventArgs>)Delegate.Combine(PassFileEventHandlerDelegate, value);
            }
            remove
            {
                PassFileEventHandlerDelegate = (EventHandler<PassFileEventArgs>)Delegate.Remove(PassFileEventHandlerDelegate, value);
            }
        }

        /// <summary>
        ///     This is the method that is responsible for notifying
        ///     receivers that the event occurred
        /// </summary>
        protected virtual void OnPassFile(PassFileEventArgs e)
        {
            if (PassFileEventHandlerDelegate != null)
            {
                PassFileEventHandlerDelegate(this, e);
            }
        }

        #endregion //('PassFile' event definition code)

        private void tb_Allow_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnAllowFile(new AllowFileEventArgs(this));
        }

        private void tb_ReSave_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnReSaveFile(new ReSaveFileEventArgs(this));
        }

        private void tb_Pass_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnPassFile(new PassFileEventArgs(this));
        }

    }
}